import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/entrys/rp/inv_counter_details_entity.dart';
import 'package:xiongan_app_yy/pages/common/TextFieldItem.dart';
import 'package:xiongan_app_yy/provider/inv_counter_info.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';
import 'package:xiongan_app_yy/widgets/comm/scan_widget.dart';

class IdCounterAdd extends StatefulWidget {
  final Map<dynamic, dynamic> args;
  @override
  _IdCounterAddState createState() => _IdCounterAddState();

  IdCounterAdd(this.args);

}

class _IdCounterAddState extends State<IdCounterAdd> {
  List<InvCounterDetailsDatasInorderdetailsnlist> listData = List();
  bool flag = true;
  void addData() {
    setState(() {
      InvCounterDetailsDatasInorderdetailsnlist map = InvCounterDetailsDatasInorderdetailsnlist();
      map.materialsnid = "";
      this.listData.add(map);
    });
  }

  void removeData(index) {
    setState(() {
      this.listData.removeAt(index);
    });
  }

  @override
  void initState() {
    InvCounterDetailsData data=widget.args["data"];
    if(data.inOrderDetailSnList.length>0){
      for(int i=0;i<data.inOrderDetailSnList.length;i++){
        listData.add(data.inOrderDetailSnList.elementAt(i));
      }
    }else{
      InvCounterDetailsDatasInorderdetailsnlist map = InvCounterDetailsDatasInorderdetailsnlist();
      map.materialsnid = "";
      listData.add(map);
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> TextFieldList = List();
    for (int index = 0; index < listData.length; index++) {
      var item = listData[index];
      TextEditingController controller = TextEditingController();
      controller.text=item.materialsnid;
      controller.selection=TextSelection.fromPosition(TextPosition(
          affinity: TextAffinity.downstream,
          offset: controller.text.length));
      if (index == 0) {
        TextFieldList.add( Container(
          color: Colors.white,
            padding: EdgeInsets.all(10),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
            Container(padding: EdgeInsets.only(right: 10),child: Text(
              "实物ID:",
              style: TextStyle(color: Color(0xff323232), fontSize: 16),
            ),),

                Expanded(
                    child: Container(
                      width: double.infinity,
                      padding: EdgeInsets.only(left: 10),
                      decoration: BoxDecoration(
                        border:
                        Border.all(color: Color(0xffefefef), width: 0.5),
                        borderRadius: new BorderRadius.circular((5.0)), // 圆角度
                      ),
                      child: TextField(
                          inputFormatters: [
                            WhitelistingTextInputFormatter(RegExp(
                                "[a-zA-Z]|[0-9]")),
                          ],
                          onChanged: (val) {
                            if(val==""){
                              flag=false;
                            }else{
                              flag=true;
                            }
                            item.materialsnid = val;

                          },
                          controller: controller,
                          decoration: InputDecoration(
                            suffixIcon: ScanWidget(clickCallback: (value){
                              setState(() {
                                item.materialsnid = value;
                              });
                            },),
                              hintText: "实物ID",
                              hintStyle: TextStyle(fontSize: 16),
                              border: InputBorder.none,)),
                    )),
                Container(
                  width: 88,
                  height: 48,
                  padding: const EdgeInsets.only(left: 10),
                  child: FlatButton(
                      onPressed: () {
                        setState(() {
                          addData();
                        });
                      },
                      color: Colors.blue,
                      child: Text(
                        "+ 新增",
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      )),
                ),
              ],
            )),
        );
      } else {
        TextFieldList.add(Container(
            color: Colors.white,
            padding: EdgeInsets.all(10),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(padding: EdgeInsets.only(right: 10),child: Text(
                  "实物ID:",
                  style: TextStyle(color: Color(0xff323232), fontSize: 16),
                ),),

                Expanded(
                    child: Container(
                      width: double.infinity,
                      padding: EdgeInsets.only(left: 10),
                      decoration: BoxDecoration(
                        border:
                        Border.all(color: Color(0xffefefef), width: 0.5),
                        borderRadius: new BorderRadius.circular((5.0)), // 圆角度
                      ),
                      child: TextField(
                          inputFormatters: [
                            WhitelistingTextInputFormatter(RegExp(
                                "[a-zA-Z]|[0-9]")),
                          ],
                          onChanged: (val) {
                            if(val==""){
                              flag=false;
                            }else{
                              flag=true;
                            }
                            item.materialsnid = val;
                          },
                          controller: controller,
                          decoration: InputDecoration(
                            suffixIcon: ScanWidget(clickCallback: (value){
                              setState(() {
                                item.materialsnid = value;
                              });
                            },),
                            hintText: "实物ID",
                            hintStyle: TextStyle(fontSize: 16),
                            border: InputBorder.none,)),
                    )),
                Container(
                  width: 88,
                  height: 48,
                  padding: const EdgeInsets.only(left: 10),
                  child: FlatButton(
                      onPressed: () {
                        setState(() {
                          removeData(index);
                        });
                      },
                      color: Colors.blue,
                      child: Text(
                        "- 删除",
                        style: TextStyle(color: Colors.white, fontSize: 16),
                      )),
                ),
              ],
            )));
      }
    }
    return Scaffold(
      appBar: new AppBar(
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        backgroundColor: Color(0XFF00A5F1),
        title: Text(
          "维护实物ID",
          style: TextStyle(fontSize: 18),
        ),
        elevation: 0,
        centerTitle: true,
      ),
      body: Column(children: <Widget>[
        Expanded(child: Scrollbar(
        child: SingleChildScrollView(child:Column(children: TextFieldList,))),

        ),
        Container(
          width: double.infinity,
          height: 48,
          color: Color(0xff00A5F1),
          child: FlatButton(
              onPressed: () {
                for(int i = 0; i < listData.length; i++){
                  if(listData.elementAt(i).materialsnid==""){
                    flag=false;
                  }
                }
                if(flag){
                  Provider.of<CounterInfo>(context).setMaterialsnid(listData, widget.args["index"]);
                  Navigator.pop(context);
                }else{
                  TostUtil.showToast("请输入完整实物ID");
                }

              },
              child: Text(
                "确定",
                style: TextStyle(color: Colors.white, fontSize: 20),
              )),
        )
      ],),
    );
  }
}
